package com.jf.es.sql.restapi.bboss;


import com.jf.es.sql.restapi.bboss.domain.DemoDto;
import lombok.extern.slf4j.Slf4j;
import org.frameworkset.elasticsearch.boot.BBossESStarter;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("es/sql/rest/api/bboss")
public class BBossController {

    @Autowired
    private BBossESStarter bbossESStarter;

    @GetMapping("createIndice")
    public ResponseEntity<Object> test2(String indice) {
        ClientInterface clientUtil = bbossESStarter.getRestClient();
        final String createIndice = clientUtil.createIndiceMapping(indice, null);
        log.info(" result: {}", createIndice);
        return ResponseEntity.ok(createIndice);
    }

    @GetMapping("sql")
    public ResponseEntity<Object> sql() {
        // todo 方法异常，暂未修复
        ClientInterface clientUtil = bbossESStarter.getRestClient();
        final List<DemoDto> list = clientUtil.sql(DemoDto.class, "SELECT * FROM library WHERE release_date < '3000-01-01'");
        log.info(" result: {}", list);
        return ResponseEntity.ok(list);
    }
}
